package com.cn.why.film.filmindex.controller;

import com.cn.why.film.filmindex.entity.Produce;
import com.cn.why.film.filmindex.service.ProducesService;
import com.cn.why.film.filmindex.util.RedisUtil;
import com.cn.why.film.filmindex.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * BarController
 */
@Controller
@Api("Bar商品展示及活动秒杀")
public class BarController {

    @Autowired
    private ProducesService service;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * swagger测试方法
     * @param id
     * @return
     */
    @ApiOperation(value = "测试swagger",notes = "哈哈哈")
    @ApiImplicitParam(name="id",value = "用户ID",required = true,dataType = "Integer",paramType = "path")
    @RequestMapping(value = "test/{id}")
    public Result testSwagger(@PathVariable(value = "id") Integer id){
        return Result.success(null);
    }

    /**
     * 请求开始
     */
    //将请求重定向
    @RequestMapping("/")
    public String index(){
        return "redirect:/list";//重定向到list方法
    }

    /**
     * 请求thymelef页面 ,redis存储热点数据
     * @param model
     * @return
     */
    @ApiOperation(value = "查询所有的商品")
    @RequestMapping("/list")
    public String listProduces(Model model){
        List<Produce> listPro = new ArrayList<>();
        //判断缓存中是否有存值
        if(redisUtil.hasKey("ProList")){
            listPro= (List<Produce>) redisUtil.getUnusall("ProList");
        }else {
            listPro = service.getProList();
            redisUtil.set("ProList",listPro);
            System.out.println("-------------------");
        }
        //存入seesion
        model.addAttribute("produces",listPro);
        return "produces/list";//跳转到templates静态页面
    }

    /**
     * 跳转到修改页面
     * @param produces
     * @return
     */
    @ApiOperation(value = "跳转到修改页面")
    @RequestMapping("/toAdd")
    public String toAdd(Produce produces){
        return "produces/addProduce";//添加完成重定向到list
    }


    /**
     * 增加
     * @param produces
     * @return
     */
    @ApiOperation(value = "增加商品种类")
    @RequestMapping("/add")
    public String add(Produce produces){
        service.save(produces);
        //更新缓存
        redisUtil.delete("ProList");
        return "redirect:/list";//添加完成重定向到list
    }

    /**
     * 根据Id修改
     * @param model
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id修改商品信息")
    @RequestMapping("/toEdit")
    public String toEdit(Model model,Integer id){
        //根据id获取到产品
        Produce produce = service.findProById(id);
        model.addAttribute("produce",produce);
        return "produces/produceEdit";//跳转到produceEdit.html
    }

    /**
     * 修改数据
     * @param produce
     * @return
     */
    @ApiOperation(value = "修改商品")
    @RequestMapping("/edit")
    public String edit(Produce produce){
        //修改数据
        service.edit(produce);
        return "redirect:/list";//获取列表数据并显示
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除商品信息")
    @RequestMapping("/delete")
    public String deletePro(Integer id){
        //根据id删除
        service.delete(id);
        //删除数据后更新到缓存中
        redisUtil.delete("ProList");
        return "redirect:/list";//获取列表数据并显示
    }

    /**
     * 批量出入数据
     * @return
     */
    @ApiOperation("批量插入商品数据")
    @RequestMapping("/muchInsert")
    public String forAddProduces(){
        service.forAddProduces();
        return "redirect:/list";//再次请求查询所有，跳转到查询页面
    }

    /*********************************************WebSocket相关*********************************************/
    /**
     * 广播推送，所有用户都能看得见
     * @param msg
     * @return
     */
    @MessageMapping("/handle2")
    @SendTo("/topic/greetings2")
    public String sendAll(String msg){
        return "服务端已收到信息："+msg;
    }

    /**
     * 广播推送，指定用户能听见
     * @return
     */
    @MessageMapping("/handle1")
    @SendToUser(value = "/topic/greetings1")
    public String sendUser(String userId){
        return "服务端已收到指定用户信息："+userId;
    }
}
